<template>
<div>
  <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

    <el-table-column
        prop="name"
        label="菜单名称"
        sortable
        width="180">
    </el-table-column>
    <el-table-column
        prop="id"
        label="id"
        sortable
        width="80">
    </el-table-column>
    <el-table-column
        prop="path"
        label="地址">
    </el-table-column>
    <el-table-column
        prop="parentId"
        label="父id">
    </el-table-column>
    <el-table-column
        fixed="right"
        label="操作"
        width="150">
      <template slot-scope="scope">
        <el-button @click="openEdit(scope.row)" type="text" size="small">编辑</el-button>
        <el-button @click="openAdd(scope.row)" type="text" size="small">增加</el-button>
        <el-button @click="delById(scope.row.id)" type="text" size="small">删除</el-button>
      </template>
    </el-table-column>


  </el-table>


  <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible">
    <el-form :model="editForm">
      <el-form-item label="菜单名称" :label-width="formLabelWidth">
        <el-input v-model="editForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单地址" :label-width="formLabelWidth">
        <el-input v-model="editForm.path" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单组件" :label-width="formLabelWidth">
        <el-input v-model="editForm.component" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="edit">确 定</el-button>
    </div>
  </el-dialog>


  <el-dialog title="增加菜单" :visible.sync="dialogFormVisible2">

    <el-form :model="addForm">
      <el-form-item label="父菜单" :label-width="formLabelWidth">
        <el-input disabled="disabled" v-model="addForm.parentId" autocomplete="off"></el-input>
        <el-input  disabled="disabled" v-model="addForm.pname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单名称" :label-width="formLabelWidth">
        <el-input v-model="addForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单地址" :label-width="formLabelWidth">
        <el-input v-model="addForm.path" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="菜单组件" :label-width="formLabelWidth">
        <el-input v-model="addForm.component" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible2 = false">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "MenuView",
  data() {
    return{
      tableData: [],
      dialogFormVisible:false,
      dialogFormVisible2:false,
      formLabelWidth:'120px',
      editForm:{},
      addForm:{},
    }
  },methods:{
    delById(id){

     let b= confirm("确定删除吗？");
    if(b) {
      //删除前需要判断该菜单是否是叶子菜单（）
      this.axios.get("menu/getMenuByPid", {params: {id: id}}).then(res => {
        if (res.data.code != 200) {
          this.$message.warning("该菜单下有子菜单不能删除");
        } else {
          this.axios.get("menu/delById", {params: {id: id}}).then(res => {
            if (res.data.code == 200) {
              this.$message.success("删除成功");
              this.list();
            } else {
              this.$message.error("删除失败");
            }
          })

        }
      })
    }
    },

    openAdd(row){
     this.dialogFormVisible2=true;
     this.addForm.parentId=row.id;
     this.addForm.pname=row.name;

    },
    add(){
      this.axios.post("menu/saveOrUpdate",this.addForm).then(res=>{
        if(res.data.code==200){
          this.$message.success("保存成功");
          this.dialogFormVisible2=false;
          this.list();
        }else{
          this.$message.success("保存失败")
        }
      })

    }
    ,
    edit(){
      this.axios.post("menu/saveOrUpdate",this.editForm).then(res=>{
        if(res.data.code==200){
          this.$message.success("保存成功");
          this.dialogFormVisible=false;
        }else{
          this.$message.success("保存失败")
        }
      })

    },
    openEdit(row){
      this.editForm =row
      this.dialogFormVisible=true;
    },

    list(){
      this.axios.get('menu/list').then(res=>{
        this.tableData=res.data.data;
      })
    }
},created() {
    this.list();
  }
}
</script>

<style scoped>

</style>